package com.km.service;

import com.km.entity.Article;
import com.km.repository.ArticleRepository;
import com.km.repository.ArticleTagsRepository;
import com.km.repository.TagsRepository;
import com.km.util.DateUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2017/10/10.
 */
@Service
public class ArticleService {

    @Resource
    private ArticleRepository articleRepository;
    @Resource
    private TagsRepository tagsRepository;

    public Page<Article> findAll(int page, int size) {
        Pageable pageable = new PageRequest(page, size,  new Sort(Sort.Direction.DESC, "id"));
        Page<Article> articlePage = articleRepository.findAll(pageable);
        articlePage.getContent().forEach(article -> {
            article.setCreateDate(DateUtil.dateToString(article.getCreateTime()));
            article.setTagsList(tagsRepository.findTagsByArticleId(article.getId()));
        });
//        new Sort(Sort.Direction.DESC, "id"))
        return articlePage;
    }
}
